package com.cskaoyan.gateway.controller.user;


import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.promo.PromoService;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;

import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * @Author: DLX
 * @Date: 2021/6/20 15:50
 * @Description:
 **/
@RestController
@RequestMapping("/user")
public class LoginController {

    @Reference(check = false)
    private IKaptchaService iKaptchaService;
    @Reference
    private PromoService promoService;

    @Reference(check = false, retries = 0)
    private ILoginService loginService;

    // 登录
    @PostMapping("/login")
    @Anoymous
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse httpServletResponse) {
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        // 验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = response.getCode();
        if (!code.equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        // 登录
        UserLoginRequest loginRequest = new UserLoginRequest();
        loginRequest.setUserName(userName);
        loginRequest.setPassword(userPwd);
        UserLoginResponse loginResponse = loginService.login(loginRequest);
        // 成功
        if (loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Cookie cookie= CookieUtil.genCookie("access_token",loginResponse.getToken(),"/",7200);
            //maxAge 的单位是秒
            cookie.setHttpOnly(true);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil<>().setData(loginResponse);
        }
        // 失败
        return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
    }


    // 验证登录
    @GetMapping("/login")
    public ResponseData verifyLogin(HttpServletRequest request) {
        System.out.println(request);
        String userInfo = ((String) request.getAttribute(TokenIntercepter.USER_INFO_KEY));
        System.out.println(userInfo);
        System.out.println(userInfo);
        UserLoginVerifyResponse loginVerifyResponse = loginService.verifyLogin(userInfo);
        if (loginVerifyResponse != null) {
            return new ResponseUtil<>().setData(loginVerifyResponse);
        }
        return new ResponseUtil<>().setErrorMsg("token已失效1");
    }
}